
function GDT___MODULE_ID__() {
  
  this.updatetxtcolor = function(eltId, colorname) {
   _gel(eltId).style.color = colorname;
  }

  this.prefs = new _IG_Prefs(__MODULE_ID__);
  var site = 'http://datemulti.googlecode.com/svn/trunk/'; // Site de stockage des images
  
  var couleur = new Array();
		 couleur["white"] = "ffffff";
		 couleur["black"] = "000000";
 		couleur["softblue"] = "99CCFF";
 		couleur["softblue"] = "99CCFF";
 		couleur["softpink"] = "FF99CC";
 		couleur["pink"] = "FFC0CB";
 		couleur["softsand"] = "FFCC99";
		 couleur["softsun"] = "FFFF99";
 		couleur["softgreen"] = "ADDA9A";
 		couleur["softpurple"] = "CC99FF";
 		couleur["medpurple"] = "993366";
 		couleur["slateblue"] = "6A5ACD";
 		couleur["medaqua"] = "00CCFF";
 		couleur["aqua"] = "00FFFF";
		 couleur["lime"] = "00FF00";
		 couleur["fuchsia"] = "FF00FF";
		 couleur["red"] = "FF0000";
 		couleur["darkyellow"] = "FF9900";
		 couleur["medgreen"] = "99CC00";
 		couleur["bluegreen"] = "339966";
		 couleur["medblue"] = "33CCCC";
		 couleur["darkblue"] = "3366FF";
		 couleur["purple"] = "800080";
		 couleur["blue"] = "0000FF";
		 couleur["teal"] = "008080";
		 couleur["green"] = "008000";
		 couleur["olive"] = "808000";
		 couleur["greenbrown"] = "FF6600";
		 couleur["maroon"] = "800000";
		 couleur["brown"] = "993300";
		 couleur["draknavy"] = "003366";
		 couleur["navy"] = "000080";
		 couleur["softnavy"] = "333399";
		 
		 
  var clockColor = (this.prefs.getString('color')) ? '#'+couleur[this.prefs.getString('color')] : '#'+couleur["softgreen"];
  this.textColor = (this.prefs.getString('txtcolor')) ? '#'+couleur[this.prefs.getString('txtcolor')] : '#'+couleur["white"];
  this.clockLang = (this.prefs.getString('lang')) ? this.prefs.getString('lang') : 'french';
  var clockformat = (this.prefs.getString('formattime')) ? this.prefs.getString('formattime') : '24:00';
  this.dispDay = -1;
  
  this.updatetxtcolor('gdt_main___MODULE_ID__',this.textColor);

  // Static globals.
  switch(this.clockLang) {
   case 'Spanish / Espagnol':
       this.monthsArray = new Array("ENR", "FEB", "MAR", "ABR", "MAY", "JUN", "JUL", "AGO", "SEP", "OCT", "NOV", "DIC");
	   this.daysArray = new Array("Dom", "Lun", "Mar", "Mié", "Jue", "Vie", "Sáb");
       break;
   case 'German / Allemand':
       this.monthsArray = new Array("JAN", "FEB", "MÄR", "APR", "MAI", "JUN", "JUL", "AUG", "SEP", "OKT", "NOV", "DEZ");
       this.daysArray = new Array("Son", "Mon", "Die", "Mit", "Don", "Fre", "Sam");
       break;
   case 'English / Anglais':
       this.monthsArray = new Array("JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC");
       this.daysArray = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
       break;
   case 'Dutch / Hollandais':
       this.monthsArray = new Array("JAN", "FEB", "MAR", "APR", "MEI", "JUN", "JUL", "AUG", "SEP", "OKT", "NOV", "DEC");
       this.daysArray = new Array("Zon", "Maa", "Din", "Woe", "Don", "Vri", "Zat");
       break;
   default:
       this.monthsArray = new Array("JAN", "FEV", "MAR", "AVR", "MAI", "JUI", "JUL", "AOU", "SEP", "OCT", "NOV", "DEC");
       this.daysArray = new Array("Dim", "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam");
       break;
  }

  this.mhOffset = -90;
  this.hhOffset = -90;

  // Browser detection.
  this.agt = navigator.userAgent.toLowerCase();
  this.is_ie = (this.agt.indexOf("msie")!= -1 && document.all && this.agt.indexOf("opera") == -1);
  this.is_ie5 = this.is_ie && (this.agt.indexOf("msie 5") != -1);
  this.is_ie6 = this.is_ie && (this.agt.indexOf("msie 6") != -1);
  this.is_ie7 = this.is_ie && (this.agt.indexOf("msie 7") != -1);

  /**
   * Writes an element's html for browsers with getElementById()
   */
  this.writeElement = function(eltId, str) {
    _gel(eltId).innerHTML = str;
  }
  
  /**
   * Changes the clipping (position) of an element's background image for browsers with getElementById()
   */
  this.updateX = function(eltId, xCoord) {
   _gel(eltId).style.left = '' + xCoord + 'px';
  }

  /**
   * Decide whether there is enough room to show the calendar.
   */
  this.toggleCalDisp = function() {
    /*if (_gel('gdt_main___MODULE_ID__').clientWidth > 315) {
      _gel('gdt_cal___MODULE_ID__').style.display = 'block';
    } else {
      _gel('gdt_cal___MODULE_ID__').style.display = 'none';
    }*/
    _gel('gdt_cal___MODULE_ID__').style.display = 'block';
  }

  /**
   * Save the background image.
   */
  this.setBkgPng = function (elId, imgSrc, repeat, color) {
    _gel(elId).style.backgroundImage = 'url(\'' + imgSrc + '\')';
    _gel(elId).style.backgroundRepeat = repeat;
	if(color){
    	_gel(elId).style.backgroundColor = color;
	}
  }

  /**
   * Apply a filter instead of saving the background image.
   */
  this.fixIeBkgPng = function (elId, imgSrc, repeat, color) {
    _gel(elId).style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, src=\''+imgSrc+'\'';
    _gel(elId).style.backgroundImage = 'none';
	if(color){
    	_gel(elId).style.backgroundColor = color;
	}
  }

  /**
   * Updates the clock to the current time.
   */
  this.upClock = function() {
	//var timeview = hours+":"+minutes+":"+seconds;
	//var timeview = hours+":"+minutes;
	var timeview = "00:00";
	var ctime = new Date();
	var phr = ctime.getHours();
	var pmin = ctime.getMinutes();
	var psec = ctime.getSeconds();
    var pday = ctime.getDay();
    var pmonth = ctime.getMonth();
    var pdate = ctime.getDate();
	
	hours = (phr < 10) ? "0"+phr : phr;
	minutes = (pmin < 10) ? "0"+pmin : pmin;
	seconds = (psec < 10) ? "0"+psec : psec;
		
  switch(clockformat) {
   case '--------------------':
		timeview = "";
		break;
   case '12:00':
		// scale clock to 0 - 11
		if (phr >= 12) phr -= 12;
		timeview = phr+":"+minutes;
		break;
   case '12:00:00':
		// scale clock to 0 - 11
		if (phr >= 12) phr -= 12;
		hours = (phr < 10) ? "0"+phr : phr;
		timeview = hours+":"+minutes+":"+seconds;
		break;
   case '24:00:00':
		timeview = hours+":"+minutes+":"+seconds;
		if (phr >= 12) phr -= 12;
		break;
   default:
		timeview = hours+":"+minutes;
		if (phr >= 12) phr -= 12;
		break;
  }


    // adjust background positions for hands... with adjustments to fix slight deviations in position.
    var minVal = (phr * 60 + pmin) / 720;
    var hrAdjust = Math.round(Math.sin(minVal * 2 * Math.PI) * 10);
    var minAdjust = Math.round(Math.sin(pmin / 60 * 2 * Math.PI) * 20);

    this.updateX('gdt_hands_h___MODULE_ID__', Math.round(minVal * 36) * this.hhOffset + hrAdjust );
    this.updateX('gdt_hands_m___MODULE_ID__', pmin * this.mhOffset + minAdjust);
	
	if(this.prefs.getString('affcalendar') == 'true' || this.prefs.getString('affcalendar') == true){
		this.drawCal(pday, pmonth, pdate, ctime.getYear());
	}
	
    // update date if necessary

      this.writeElement('gdt_day___MODULE_ID__', this.daysArray[pday]);
      this.writeElement('gdt_month___MODULE_ID__', this.monthsArray[pmonth]);
      this.writeElement('gdt_date___MODULE_ID__', pdate);
	  
      this.writeElement('gdt_time___MODULE_ID__', timeview);
      this.dispDay = pdate;
      
	if (this.prefs.getString('affactualtime') == false) {
    	_gel('gdt_ba_date___MODULE_ID__').style.display = "none";
    	_gel('gdt_bb_date___MODULE_ID__').style.visibility = "";
    }else{
    	_gel('gdt_ba_date___MODULE_ID__').style.visibility = " ";
    	_gel('gdt_bb_date___MODULE_ID__').style.visibility = "none";	
    }
  }

  /**
   * draw calendar
   */
  this.drawCal = function (day, month, date, year) {
    this.clockLang = (this.prefs.getString('lang')) ? this.prefs.getString('lang') : 'french';

    // Static globals.
    switch(this.clockLang) {
     case 'Spanish / Espagnol':
       var days_of_week = ['D', 'L', 'M', 'M', 'J', 'V', 'S'];
       break;
     case 'German / Allemand':
       var days_of_week = ['S', 'M', 'D', 'M', 'D', 'F', 'S'];
       break;
	 case 'English / Anglais':
         var days_of_week = ['S', 'M', 'T', 'W', 'T', 'F', 'S'];
         break;
	 case 'Dutch / Hollandais':
         var days_of_week = ['Z', 'M', 'D', 'W', 'D', 'V', 'Z'];
         break;
     default:
         var days_of_week = ['D', 'L', 'M', 'M', 'J', 'V', 'S'];
         break;
    }
	
    var num_days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    var result = '<table style="margin-top: 10px;" cellspacing="1" cellpadding="2" border="0"><tr>';
    var num_days = num_days_in_month[month];
    if (month == 1 && year % 4 == 0) {
      if (year % 100 != 0 || year % 400 == 0) {
        num_days = 29;
      }
    }

    for (var i = 0; i < 7; i++) {
      result += '<td style="font-size: 9px; color: '+this.textColor+'; border-bottom: 1px dotted '+this.textColor+';">' +
        days_of_week[i] + '</td>';
    }
    result += '</tr>';

    var curr_day = 1;
    var starting_day = ((day - date % 7 + 1) + 7 )% 7;
    for (var i = 0; i < 6; i++) {
      result += '<tr>';
      for (var j = 0; j < 7; j++) {
        var style = 'font-size: 9px; color: '+this.textColor+';';
        var content = curr_day;
        if ((i == 0 && j < starting_day) || curr_day > num_days) {
          content = '&nbsp;';
        } else {
          if (curr_day == date) {
            style += ' border: 1px solid '+this.textColor+'; color: '+this.textColor+'; font-weight: bold;';
          }
          curr_day++;
        }
        result += '<td style="' + style + '">' + content + '</td>';
      }
      result += '</tr>';
    }
    result += '</table>';
    _gel('gdt_cal___MODULE_ID__').innerHTML = result;
  }

  /**
   * Sets up the clock to update every minute.
   */
  this.startInterval = function() {
    window.setInterval('GDT___MODULE_ID___handle.upClock()',1000);
    this.upClock();
  }

  this.setImages = function() {
  
    if (this.is_ie5 || this.is_ie6) {
      this.fixIeBkgPng('gdt_clock___MODULE_ID__', site+'clock.png', 'no-repeat', false);
      this.fixIeBkgPng('gdt_l___MODULE_ID__', site+'left.png', 'no-repeat', clockColor);
      this.fixIeBkgPng('gdt_r___MODULE_ID__', site+'right.png', 'no-repeat', clockColor);
      this.setBkgPng('gdt_b_clock___MODULE_ID__', site+'bg_p.png', 'repeat-x', clockColor);
      this.setBkgPng('gdt_b_date___MODULE_ID__', site+'bg_p.png', 'repeat-x', clockColor);
      //this.setBkgPng('gdt_c_date___MODULE_ID__', site+'bg_p.png', 'repeat-x', clockColor);
      this.setBkgPng('gdt_cal___MODULE_ID__', site+'bg_p.png', 'repeat-x', clockColor);
      this.fixIeBkgPng('gdt_hands_m___MODULE_ID__', site+'hm.png', 'no-repeat', false);
      this.fixIeBkgPng('gdt_hands_h___MODULE_ID__', site+'hh.png', 'no-repeat', false);
    } else {
      this.setBkgPng('gdt_clock___MODULE_ID__', site+'clock.png', 'no-repeat', false);
      this.setBkgPng('gdt_l___MODULE_ID__', site+'left.png', 'no-repeat', clockColor);
      this.setBkgPng('gdt_r___MODULE_ID__', site+'right.png', 'no-repeat', clockColor);
      this.setBkgPng('gdt_b_clock___MODULE_ID__', site+'bg_p.png', 'repeat-x', clockColor);
      this.setBkgPng('gdt_b_date___MODULE_ID__', site+'bg_p.png', 'repeat-x', clockColor);
      //this.setBkgPng('gdt_c_date___MODULE_ID__', site+'bg_p.png', 'repeat-x', clockColor);
      this.setBkgPng('gdt_cal___MODULE_ID__', site+'bg_p.png', 'repeat-x', clockColor);
      this.setBkgPng('gdt_hands_m___MODULE_ID__', site+'hm.png', 'no-repeat', false);
      this.setBkgPng('gdt_hands_h___MODULE_ID__', site+'hh.png', 'no-repeat', false);
    }
  }

  /**
   * Sets up the clock, and tries its best to sync with the system clock to update at the right time.
   */
  this.main = function() {
    // address differences in relatively positioned elements in different browsers
    if (!this.is_ie5 && !this.is_ie6 && !this.is_ie7) {
      _gel('gdt_hands___MODULE_ID__').style.position = 'relative';
    }
    this.setImages();
    if (window.addEventListener) {
      window.addEventListener('resize', GDT___MODULE_ID___handle.toggleCalDisp, false);
      window.addEventListener('load', GDT___MODULE_ID___handle.toggleCalDisp, false);
    } else if (window.attachEvent) {
      window.attachEvent('onresize', GDT___MODULE_ID___handle.toggleCalDisp);
      window.attachEvent('onload', GDT___MODULE_ID___handle.toggleCalDisp);
    }
    this.upClock();
    window.setTimeout('GDT___MODULE_ID___handle.startInterval()', 1000);
    //window.setTimeout('GDT___MODULE_ID___handle.startInterval()', (60 - new Date().getSeconds()) * 1000 + 5);
    //window.setTimeout('GDT___MODULE_ID___handle.startInterval()', 100);
  }
}

var GDT___MODULE_ID___handle = new GDT___MODULE_ID__();

_IG_RegisterOnloadHandler(function() {
  GDT___MODULE_ID___handle.main();
});

